package com.example.xuecheng_content.controller;

import config.TokenConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.SecurityUserUtil;

import java.io.IOException;

/**
 * @author Mr.M
 * @version 1.0
 * @description 测试controller
 * @date 2022/9/27 17:25
 */
@RestController
@PropertySource(value = {"classpath:config.properties"}, encoding = "utf-8")
@RequestMapping("/content")
public class LoginController {
//
//    @Autowired
//    XcUserMapper userMapper;
     @Autowired
    TokenConfig tokenConfig;
    @RequestMapping("/login-success")
    public String loginSuccess(){
        return "登录成功"   ;
    }




    @RequestMapping("/r/r1")
//    @PreAuthorize("hasAuthority('p1')")
    public String r1() {
        // 方式一：
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 方式二：
        //springsecurity用户名替换成json字符串 之后
        SecurityUserUtil.XcUser user = SecurityUserUtil.getUser();
        System.out.println("名字是："+user);
        return "访问r1资源";
    }

    @RequestMapping("/r/r2")
    @PreAuthorize("hasAuthority('p2')")
    public String r2() {
        return "访问r2资源";
    }

    @RequestMapping("/test")
    public String test() {
        return "访问test资源";
    }


//    @RequestMapping("/user/{id}")
//    public XcUser getuser(@PathVariable("id") String id) {
//        XcUser xcUser = userMapper.selectById(id);LoginController
//        return xcUser;
//    }
private final static Logger log = (Logger) LoggerFactory.getLogger(LoginController.class);
    @Value("${springName}")
    private String springName;
    @RequestMapping("/test2")
    public String  test2() throws IOException {
        System.out.println(springName);
        log.info("ceshi");
        return "logger 测试";
    }

}
